# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>
# ArchLinux maintainer: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: dibblethewrecker dibblethewrecker.at.jiwe.dot.org
# Contributor: William Rea <sillywilly@gmail.com>
# Contributor: Petri Moisio <petri.k.moisio@gmail.com>

_realname=gdal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.3
pkgrel=4
pkgdesc="A translator library for raster geospatial data formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://gdal.org/"
license=('custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-postgresql")
depends=("${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-crypto++"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-libfreexl"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libkml"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-proj"
         #"${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         $([[ ${MINGW_PACKAGE_PREFIX} = *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-xerces-c")
         "${MINGW_PACKAGE_PREFIX}-xz")
#optdepends=("${MINGW_PACKAGE_PREFIX}-postgresql")
options=('strip' 'staticlibs')
source=(https://download.osgeo.org/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz
        001-qhull-reentrant.patch
        002-dont-define-msvcrt-version.patch)
sha256sums=('1e8fc8b19c77238c7f4c27857d04857b65d8b7e8050d3aac256d70fa48a21e76'
            'd331a4ca57ba9ecd533d2705273ca89b472902a880eb9edae9d0766bc8034519'
            '2eb8b8d7321f5a75cc7ff29bf63ae8bda1c900f463b6577bac109503346275f4')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-qhull-reentrant.patch
  patch -p1 -i ${srcdir}/002-dont-define-msvcrt-version.patch

  cd ${srcdir}
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM}
  cd "${srcdir}/build-${MSYSTEM}"

  sed -i "s|/usr/local|${MINGW_PREFIX}|g" configure.ac
  sed -i "s|/usr|${MINGW_PREFIX}|g" configure.ac
  sed -i "s|mandir='\${prefix}/man'|mandir='\${prefix}/share/man'|g" configure.ac
  for p in m4/*.m4
  do
    sed -i "s|/usr|${MINGW_PREFIX}|g" $p
  done
  # bug: http://osgeo-org.1560.x6.nabble.com/gdal-dev-jpeg2000-jasper-error-compiling-gdal-2-1-from-git-release-branch-td5299100.html
  sed -i -e 's@uchar@unsigned char@' frmts/jpeg2000/jpeg2000_vsil_io.cpp

  touch config.rpath
  # for some reason autogen.sh's libtoolize call is commented out
  # but we need updated libtool for clang support
  libtoolize --force --copy
  ./autogen.sh
}

build() {
  cd "${srcdir}/build-${MSYSTEM}"

  CFLAGS+=" -fno-strict-aliasing"
  CXXFLAGS+=" -std=c++17 -Wno-deprecated-declarations"

  # somehow the configure test for redeclaring sprintf succeeds but
  # then errors during the build with clang.
  CPPFLAGS+=" -DDONT_DEPRECATE_SPRINTF"

  ./configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --with-poppler \
    --with-webp \
    --with-spatialite \
    --with-liblzma \
    --with-mysql=${MINGW_PREFIX}/bin/mariadb_config \
    --without-python \
    --without-perl \
    --with-qhull=internal

  sed -i GDALmake.opt -e "s|EXE_DEP_LIBS.*|EXE_DEP_LIBS = \$\(GDAL_SLIB\)|g"
  sed -i GNUmakefile -e "s|\$(GDAL_ROOT)\/||g"

  make
  # make man
}

package () {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  make DESTDIR="${pkgdir}" install-man

  # install license
  install -D -m644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
